import processing.opengl.*;
import processing.pdf.*;
import toxi.geom.*;
import unwrap.*;

Unwrap u;
ArrayList t;
Vec3D cam = new Vec3D(0,0,0);
PGraphicsPDF pdf;

void setup() {
  size(1024,768, OPENGL);
  pdf = (PGraphicsPDF) createGraphics(width, height, PDF, "unwrapping.pdf"); // initialise PDF
  
  update();
}

void update() {
  u = new Unwrap(this,createFont("Verdana",48)); // construct a new unwrapper

  u.beginStrip(); // begin triangleStrip
  u.addPoint(new Vec3D()); // add new Point to the triangleStrip
  u.addPoint(new Vec3D(0,-200,0));
  u.addPoint(new Vec3D(200,0,0));
  u.addPoint(new Vec3D(200,-300,0));
  u.addPoint(new Vec3D(200,0,-200));
  u.addPoint(new Vec3D(200,-200,-200));
  u.endStrip(CLOSE); // end triangleStrip
}


void draw() {
  background(200);
  stroke(255);
  fill(200);
  lights();

  // camera
  float d=frameCount;
  cam.x=sin(radians(d))*mouseX*4;
  cam.y=mouseY*4-height;
  cam.z=cos(radians(d))*mouseX*4;
  camera(cam.x,cam.y,cam.z,0,0,0,0,1,0);

  //////////////////// draw 3d
  u.draw3d(); // draw triangles in 3d
  u.drawNormals(); // draw triangle normals

  //////////////////// draw 2d
  camera();
  noFill();
  
  t = u.triangles; // get the 2d Triangles
  for(Iterator i=t.iterator(); i.hasNext();) { // loop the triangle arraylist
    UTriangle tri=(UTriangle)i.next(); // get next triangle
    tri.draw2d(new Vec2D(width/2, height/2)); // set the 2d triangle and draw it on width/2, height/2
    tri.drawNeighborInfoText(10);  // draw info text for assembling
  }

}

//////////////////// save PDF
void keyReleased(){  

  PGraphicsPDF pd = (PGraphicsPDF) pdf;
  pdf.beginDraw();
  pdf.stroke(0);
  
  int idx=0;
  t = u.triangles; // get 2d Triangles
  for(Iterator i=t.iterator(); i.hasNext();) { // loop the triangle arraylist
    UTriangle tri=(UTriangle)i.next(); // get next triangle
    pdf.noFill();
    tri.draw2d(new Vec2D(width/2, height/2),pdf); // set the 2d triangle and draw it on width/2, height/2
    pdf.fill(0);
    pdf.textMode(SHAPE); // save font with outlines
    tri.drawNeighborInfoText(10,pdf); // draw info text for assembling
    pd.nextPage(); // add new Page
    println(idx++);
  }
  pdf.dispose();
  pdf.endDraw();
  println("pdf_saved");
}


void mouseReleased() {
  update();
}

